package solutions.ferpa.ztest.intf;

import br.com.telescope.adsrt.dom.DomId;
import br.com.telescope.adsrt.dom.DomText;
import br.com.telescope.adsrt.dom.DomTextLong;
import br.com.telescope.adsrt.dom.DomUpper;
import br.com.telescope.adsrt.lib.*;
import br.com.telescope.adsrt.lib.AdsException;
import br.com.telescope.t2.ModuleNavigator;
import br.com.telescope.t2.ServiceModel;
import br.com.telescope.t2.exception.UnexpectedException;
import br.com.telescope.t2.feature.DeleteFeature;
import br.com.telescope.t2.feature.InsertFeature;
import br.com.telescope.t2.feature.ListFeature;
import br.com.telescope.t2.feature.QueryFeature;
import br.com.telescope.t2.feature.SearchFeature;
import br.com.telescope.t2.feature.UpdateFeature;
import solutions.ferpa.ztest.ent.*;

/**
 * Interfaces do sistema.
 * <p>
 * Telas para administração das interfaces do sistema.
 * <P>
 * Telescope origin: RTSYSINF.CAD_INTERFACE
 * <P>
 * Base entity: RTSYSINF.INTERFACES_SISTEMAS
 * <P>
 * @author TELESCOPE - Build task T2 Web Interface Generator
 * <P>
 * @version 0.1
 */
public class CadInterface extends ServiceModel {
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    public static final String GLOBAL_NAME = "RTSYSINF.CAD_INTERFACE";
    
    @Override
    public String globalName() {
        return GLOBAL_NAME;
    }
    
    {
        setTitle("Interfaces do sistema");
    }
    
    protected EntInterfacesSistemas base;
    protected EntInterfacesSistemas rec; // NEED_TJW1_COMPATIBILITY
    
    // Record items
    private DomId aId;
    private DomUpper aNome;
    private DomText aTitulo;
    private DomTextLong aFuncoes;
    
    // Query items
    private DomText qTitulo;
    private DomUpper qNome;
    
    @Override
    public String getBaseSql() {
        return "select BASE.*"
                + " from INTERFACES_SISTEMAS BASE";
    }
    
    @Override
    public void initAttributes() {
        
        aId = new DomId();
        aId.context(ctx).name("ID").label("Id");
        
        aNome = new DomUpper();
        aNome.context(ctx).name("NOME").label("Nome da Interface");
        
        aTitulo = new DomText();
        aTitulo.context(ctx).name("TITULO").label("Título");
        
        aFuncoes = new DomTextLong();
        aFuncoes.context(ctx).name("FUNCOES").label("Funções").unbound(true);
        
        
        qTitulo = new DomText();
        qTitulo.context(ctx).name("TITULO").label("Título");
        
        qNome = new DomUpper();
        qNome.context(ctx).name("NOME").label("Nome da Interface");
    }
    
    @Override
    public String getQueryPkColumn() {
        return "BASE.ID";
    }
    
    @Override
    public void setId(String value) {
        this.aId.setValue(value);
    }
    
    @Override
    public EntInterfacesSistemas getRecord() {
        if (base == null) {
            base = new EntInterfacesSistemas(ctx);
            rec = base;
        }
        return this.base;
    }
    
    @Override
    public String getContextName() {
        return aNome.toText();
    }
    
    @Override
    public String getDefaultOrderBy() {
        return "NOME, TITULO";
    }
    
    @Override
    public ModuleNavigator getDefaultModuleNavigator() {
        return new ModInterfaces();
    }
    
    /**
     * Popula os valores de unbounds
     */
    @Override
    public void calculateUnboundValues() {
        try {
            aFuncoes.setValue(listaFuncoes());
        } catch (AdsException ex) {
            throw new UnexpectedException("running inference rules", ex);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="QUERY">
    public class FQuery extends QueryFeature {
        
        @Override
        public void initForm() {
            addField(qNome, "opt");
            addField(qTitulo, "opt");
        }
        
        {
            setFeatureName("QUERY");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="LIST">
    public class FList extends ListFeature {
        
        @Override
        public void initForm() {
            aNome.setOptional(false);
            addField(aNome, "view");
            aTitulo.setOptional(false);
            addField(aTitulo, "view");
            addField(aFuncoes, "view");
        }
        
        {
            setFeatureName("LIST");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="INSERT">
    public class FInsert extends InsertFeature {
        
        @Override
        public void initForm() {
            aNome.setOptional(false);
            addField(aNome, "rqd");
            aTitulo.setOptional(false);
            addField(aTitulo, "rqd");
        }
        
        {
            setFeatureName("INSERT");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="UPDATE">
    public class FUpdate extends UpdateFeature {
        
        @Override
        public void initForm() {
            aNome.setOptional(false);
            addField(aNome, "rqd");
            aTitulo.setOptional(false);
            addField(aTitulo, "rqd");
        }
        
        {
            setFeatureName("UPDATE");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="DELETE">
    public class FDelete extends DeleteFeature {
        
        @Override
        public void initForm() {
            addField(aNome, "view");
            addField(aTitulo, "view");
        }
        
        {
            setFeatureName("DELETE");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="SEARCH">
    public class FSearch extends SearchFeature {
        
        @Override
        public void initForm() {
            addField(qNome, "search");
            addField(qTitulo, "search");
        }
        
        {
            setFeatureName("SEARCH");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="initFeatures">
    
    @Override
    protected void initFeatures() {
        addFeature(new FQuery());
        addFeature(new FList());
        addFeature(new FInsert());
        addFeature(new FUpdate());
        addFeature(new FDelete());
        addFeature(new FSearch());
        super.initFeatures();
    }
    
    @Override
    public String getDefaultFeature() {
        return "QUERY";
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Getters and Setters">
    
    @Override
    public Long getId() {
        return aId.getValue();
    }
    
    public void setId(Long value) {
        this.aId.setValue(value);
    }
    
    public String getNome() {
        return aNome.getValue();
    }
    
    public void setNome(String value) {
        this.aNome.setValue(value);
    }
    
    public String getTitulo() {
        return aTitulo.getValue();
    }
    
    public void setTitulo(String value) {
        this.aTitulo.setValue(value);
    }
    
    public String getFuncoes() {
        return aFuncoes.getValue();
    }
    
    public void setFuncoes(String value) {
        this.aFuncoes.setValue(value);
    }
    public String getQueryNome() {
        return qNome.getQueryValue();
    }
    
    public void setQueryNome(String value) {
        this.qNome.setQueryValue(value);
    }
    public String getQueryTitulo() {
        return qTitulo.getQueryValue();
    }
    
    public void setQueryTitulo(String value) {
        this.qTitulo.setQueryValue(value);
    }
    // </editor-fold>
    
    /**
     * Lista das funções que acessam a interface.
     * <p>
     * Retorna uma lista com o nome das funções que acessam esta interface 
     * (separados por vírgula)
     */
    private String listaFuncoes() throws AdsException {
        // <user-code id="806822" parentId="752470" hashcode="199ebb98" length="364">
        StringBuilder out = new StringBuilder();
        EntFuncoesSistemas func = new EntFuncoesSistemas(ads);
        func.find("select funcao_id from interfaces_funcoes where interface_id=?");
        func.sqlSetLong(1,rec.valueOfId());
        while (func.next()) {
            if (out.length()>0) out.append(", ");
            out.append(func.valueOfFuncaoSistema());
        }
        func.close();
        return out.toString();
        // </user-code>
    }
}
